<template>
  <div class="advancedSetup">
    <Dialog :appDialog="appDialog">
      <el-form ref="ruleFormRef" :model="setForm" :rules="rules" class="demo-ruleForm">
        <p class="set_title">员工群发消息设置</p>
        <el-form-item>
          <div class="switch_box">
            <el-switch v-model="setForm.params.switch" :active-value="1" :inactive-value="2" />
            <span>开启后，{{(props.type == 5 ? '群主' : '员工')}}执行群发后，系统自动推送未触达客户清单提醒</span>
          </div>
        </el-form-item>
        <el-form-item label="提醒规则：" v-show="setForm.params.switch == 1">
          <div class="timer_box">
            <p class="text">{{(props.type == 5 ? '群主' : '员工')}}执行群发消息后，若有客户已达当日消息接受上限，则</p>
            <div class="select_time">
              <span>每</span>
              <el-input class="cust_message_min" v-model="setForm.params.execute_time" min="0" max="60" type="number">
                <template #append>
                  <el-select class="cust_message" v-model="setForm.params.execute_time_type" placeholder="请选择时间段">
                    <el-option :value="3" label="天">天</el-option>
                    <el-option :value="2" label="小时">小时</el-option>
                    <el-option :value="1" label="分钟">分钟</el-option>
                  </el-select>
                </template>
              </el-input>
              <el-time-select size="small" v-if="setForm.params.execute_time_type === 3" class="time_pick" v-model="setForm.params.execute_time_hour" start="00:00" end="23:50" step="00:10" placeholder="选择时间" />
              <span class="remind_text">提醒执行{{(props.type == 5 ? '群主' : '员工')}}未送达消息的客户清单</span>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="提醒方式：" v-show="setForm.params.switch == 1">
          <el-radio-group v-model="setForm.params.method">
            <el-radio :label="1">多群发任务汇总提醒</el-radio>
            <el-radio :label="2">单任务提醒</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="检测群发范围：" v-show="setForm.params.switch == 1">
          <el-radio-group v-model="setForm.params.range">
            <el-radio :label="1">今日群发任务上限</el-radio>
            <el-radio :label="2">最近N天内群发任务上限</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-show="setForm.params.range == 2&&setForm.params.switch == 1" prop="params.day">
          <el-input v-model="setForm.params.day" class="day_detail" min="2" type="number"></el-input>天
        </el-form-item>
      </el-form>
    </Dialog>
  </div>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineEmits, watch, onMounted } from 'vue'
  import Dialog from '@/components/Dialog.vue'
  import { getHeightSet, setHeight } from '@/api/app'
  import { ElMessage } from 'element-plus'
  import { deepCopy } from '@/utils/helper'

  const emits = defineEmits(['submit'])
  const props = defineProps(['type']) //type 1-人群包 2-客户群发 3-客户群群发 4-客户sop 5-客户群sop 6-营销规则
  const ruleFormRef = ref()
  const isShow = ref(false)
  let setForm: any = ref({
    module: props.type,
    params: {
      switch: 1, //1-开 2-关
      execute_time: 1, // 执行周期时间
      execute_time_type: 3, // 执行周期类型 1-分钟 2-小时 3-天
      execute_time_hour: '08:00', // 人群包 - 执行周期时间为3时传 具体执行时间
      method: 2,
      range: 1,//1今日群发任务上限2最近N天内群发任务上限
      day: 2
    }
  })
  let dayValid = (rule: any, value: any, callback: any) => {
    if(setForm.value.params.range == 1) {
      callback()
    }else{
      if (!value) callback(new Error('请输入最近N天内群发任务上限'))
      else if (value == 1 || value < 1) {
        callback(new Error('请输入大于1天'))
      } else callback()
    }
  }
  const rules = reactive({
    'params.day': [{ required: true, validator: dayValid, trigger: 'change' }],
  })
  const reset = () => {
    const depForm = deepCopy(setForm.value)
    Object.keys(depForm.params).forEach((key) => {
      if (key !== 'switch') depForm.params[key] = null
    })
    return depForm
  }
  const getSetInfo = () => {
    getHeightSet({ module: props.type }).then((res: any) => {
      if (res.params.switch == 2) {
        setForm.value.params.switch = res.params.switch
      } else {
        setForm.value = Object.assign(setForm.value, res)
        if(!res.params.range){
          setForm.value.params.range = 1
        }
      }
    })
  }
  const cancelHandel = () => {
    isShow.value = false
    reset()
  }
  const submitHandel = () => {
    if (!ruleFormRef.value) return
    ruleFormRef.value.validate((valid:any) => {
      if(valid){
        setHeight(setForm.value.params.switch === 1 ? setForm.value : reset()).then(() => {
          ElMessage.success('设置成功！')
          cancelHandel()
        })
      }
    })
  }
  const appDialog: any = reactive({
    visible: isShow,
    title: '高级设置',
    width: '800px',
    cancel: cancelHandel,
    submit: submitHandel,
    cancelText: '取消',
    submitText: '确定'
  })
  watch(
    () => isShow.value,
    (val) => {
      if (val && props.type) getSetInfo()
    }
  )
  defineExpose({
    isShow
  })
</script>

<style lang="scss" scoped>
  .advancedSetup {
    font-size: 14px;
    .el-form-item {
      margin: 0 0 0 4px;
      .el-form-item__label {
        padding: 0;
      }
    }
    .set_title {
      font-size: 14px;
      font-weight: 400;
      color: #33302d;
      line-height: 14px;
      padding-left: 4px;
      border-left: 3px solid #ff6b00;
    }
    .switch_box {
      display: flex;
      align-items: center;
      span {
        font-weight: 300;
        color: #969696;
        margin-left: 8px;
      }
    }
    .timer_box {
      font-weight: 400;
      .select_time {
        display: flex;
        align-items: center;
      }
      .time_pick {
        width: 130px;
      }
      .remind_text {
        margin-left: 4px;
      }
      .cust_message_min {
        width: 140px;
        margin: 0 10px;
        input {
          padding: 0 0 0 15px;
        }
        .cust_message {
          width: 75px;
        }
      }
    }
  }
  .day_detail {
    width: 80px;
    margin: 0 15px 0 100px;
  }
</style>
